#include "capScreen.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QGuiApplication>
#include <QScreen>
#include <QColor>
#include <QPainter>
#include <QPen>
#include <QString>
#include <QDebug>

CapScreen::CapScreen(QWidget * parent) : QWidget(parent,Qt::Window | Qt::FramelessWindowHint){
    this->resize(QApplication::desktop()->size());
    isMousePress = false;
    isKeyRelease = false;
    isKeyEnsure = false;
    isKeyCancel  = false;
    mRect = QRect();
    mPos = MousePoint{};
    mCapPix = QPixmap();
    mBlurPix = QPixmap();
}

CapScreen::~CapScreen(){
    isMousePress = false;
    isKeyRelease = false;
    isKeyEnsure = false;
    isKeyCancel  = false;
}

//void CapScreen::keyPressEvent(QKeyEvent *event){
//    isKeyPress = true;
//}


void CapScreen::keyReleaseEvent(QKeyEvent *event){
    switch (event->key()) {
    case Qt::Key_A:
        if (event->key() == Qt::Key_A && event->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier)){
            qDebug() << "检测到按键！！！！";
            QPixmap full = QGuiApplication::primaryScreen()->grabWindow(QApplication::desktop()->winId());
            mPos.start =  QPoint(100,100);
            mPos.end =  QPoint(full.width() -100,full.height()-100);
            update();
        }
        break;

    case Qt::Key_1:
        qDebug() << "检测到按键2！！！！";
        if (!isMousePress) {
            // 发出信号
            QPixmap curPix = mFullPix.copy(mRect);
            emit signalShotPixmap(curPix);
            // 隐藏本窗
            mPos = mousePoint{};
            this->hide();
        }else{
            isKeyRelease =true;
            isKeyEnsure = true;
        }
        break;

    case Qt::Key_Escape:
        qDebug() << "检测到按键3！！！！";
     if (isMousePress){
         isKeyRelease =true;
         isKeyCancel = true;
     }else{
         mPos = mousePoint{};
         update();
     }
        break;


    case Qt::Key_2:
         mPos = mousePoint{};
          emit signalShotPixmap(QPixmap());
         this->hide();
         break;

    default:
        break;
    }

    return;

    /*
    if (event->key() == Qt::Key_A && event->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier)){
        qDebug() << "检测到按键！！！！";
        mFullPix = QGuiApplication::primaryScreen()->grabWindow(QApplication::desktop()->winId());
        mPos.start =  QPoint(100,100);
        mPos.end =  QPoint(mFullPix.width() -100,mFullPix.height()-100);
        update();
        return;
    }


    if (event->key() == Qt::Key_Control){
        qDebug() << "检测到按键2！！！！";
        if (!isMousePress) {
            // 发出信号
            emit signalShotPixmap(mFullPix);
            // 隐藏本窗
            this->hide();
            return;
        }else{
            isKeyRelease =true;
            isKeyEnsure = true;
            return;
        }
    }

    if (event->key() == Qt::Key_Escape){
           qDebug() << "检测到按键3！！！！";
        if (isMousePress){
            isKeyRelease =true;
            isKeyCancel = true;
        }else{
            mRect = QRect();
            update();
            return;
        }
    }
    */
}

void CapScreen::showEvent(QShowEvent  *qs){
    mFullPix = QGuiApplication::primaryScreen()->grabWindow(QApplication::desktop()->winId());
    mCapPix = QPixmap(mFullPix.width(),mFullPix.height());
    mCapPix.fill(QColor(160,160,160,200));

    mBlurPix = mFullPix;

    QPainter p(&mBlurPix);
    p.drawPixmap(0,0,mCapPix);
}



void CapScreen::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QPen pen;
    pen.setColor(Qt::green);
    pen.setWidth(2);
    pen.setStyle(Qt::DotLine);
    painter.setPen(pen);

    // 模糊全屏
    painter.drawPixmap(0,0,mBlurPix);

    // 截屏区图像
    QRect curRect = QRect(mPos.start,mPos.end).normalized();
    QPoint start = curRect.topLeft();
    QPoint end = curRect.bottomRight();

    mRect = QRect(start,end);
    painter.drawRect(mRect);

    if (!mRect.isEmpty()){
        painter.drawPixmap(mRect.x(),mRect.y(),mFullPix.copy(mRect));
    }

    // 使用黄色笔绘制图像
    painter.setPen(QPen(Qt::yellow));
    painter.drawText(mRect.x() + 2 ,mRect.y() - 8,
                     QString("Pos:(%1 x %2) Size(%3 x %4)").arg(mRect.x()).arg(mRect.y()).arg(mRect.width()).arg(mRect.height()));

}


void CapScreen::mousePressEvent(QMouseEvent *menevt) {
    if (menevt->button() == Qt::LeftButton){
        isMousePress = true;
        mPos.start = menevt->pos();
    }
}

void CapScreen::mouseMoveEvent(QMouseEvent *menevt) {
    if(isMousePress){
        mPos.end = menevt->pos();
        update(); // 刷新
    }
}

void CapScreen::mouseReleaseEvent(QMouseEvent *menevt){
    if(isMousePress){
        isMousePress = false;
        if (!mRect.isEmpty()){
            QPixmap curPix = mFullPix.copy(mRect);

            if (isKeyRelease && isKeyEnsure){
                // 发出信号
                emit signalShotPixmap(curPix);
                // 隐藏本窗
                this->hide();
                mPos = mousePoint{};
                isKeyRelease = false;
                return;
            }

            if (isKeyRelease && isKeyCancel){
                mPos = mousePoint{};
                update();
                isKeyRelease = false;
                return;
            }
        }
    }
}


